home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / CAMPE.C < prev    next >
C/C++ Source or Header  |  1992-03-09  |  15KB  |  438 lines

  1. /****************************************************************************
  2.  *
  3.  *      Program name : CAMPE.C
  4.  *
  5.  *      This is the actual minimization algorithm. Variation from the actual
  6.  *    CAMP algorithm is as follows:
  7.  *        1.    Minterms with adjacency 0 or 1 is minimised first
  8.  *        2.    Select next minterm with lowest adjacency wrt b-array
  9.  *        3.  If more than 1 such minterms, select one with the lowest
  10.  *            adjacency wrt to a-array
  11.  *        4.    To shrink the CPT, select an adjacent term, mi that has the
  12.  *            largest wi AND is already covered
  13.  *        5.  If more than one such adjacent terms are covered, select one
  14.  *        with the lowest adjacency wrt to b-array
  15.  *
  16.  * --------------------------------------------------------------------------
  17.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  18.  *    University.
  19.  *
  20.  *    You are free to use, copy and distribute this software and its
  21.  *    documentation providing that:
  22.  *
  23.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  24.  *
  25.  *        IT IS NOT MODIFIED IN ANY WAY.
  26.  *
  27.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  28.  *
  29.  *    This program is provided "AS IS" without any warranty, expressed or
  30.  *    implied, including but not limited to fitness for any particular
  31.  *    purpose.
  32.  *
  33.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  34.  *    appreciated. Please send to:
  35.  *
  36.  *        Boon-Tiong Tan or Othman Bin Ahmad
  37.  *        School of EEE
  38.  *        Nanyang Technological University
  39.  *        Nanyang Avenue
  40.  *        Singapore 2263
  41.  *        Republic of Singapore
  42.  *
  43.  ***************************************************************************/
  44.  
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #define mask8 255
  49.  
  50. unsigned char   *campe(a, b)              /* pointer to a & b array passed */
  51. unsigned char   *a, *b;
  52.  
  53. {
  54.    unsigned short   pterm, ma, mb, *pm, *pm1, pos, pos1;
  55.    unsigned  long   m, k, count, count1, topow();
  56.    register  long   i, wo, wi;
  57.    register  char   j;
  58.          char   test;
  59.    unsigned  char   *c, *d, *e, *s, *temp, adj0, adji;
  60.    unsigned  char   n, adj, adjacency(), nspm, cover;
  61.    unsigned  char   *adjacent(), *reduce(), *cpt(), *ssm(), adj_of_mi();
  62.  
  63.  
  64.    mb = *(b+2)<<8 | *(b+1);            /* no. of minterms in b-array */
  65.  
  66.    n    = *a;                          /* no. of variables */
  67.    nspm = *(a+3);                      /* no. of bytes/minterm */
  68.    ma = *(a+2)<<8 | *(a+1);            /* no. of minterms in a-array */
  69.  
  70.    temp = (unsigned char *) malloc(nspm+1);        /* temporary storage */
  71.    if (temp == 0)
  72.       {
  73.      printf("Out of memory -- CAMPE, *temp\n");
  74.      printf("Program terminated - 1\n");
  75.      exit(0);
  76.       }
  77.  
  78.    s = (unsigned char *) malloc(4);                /* header of s-array */
  79.    if (s == 0)
  80.       {
  81.      printf("Out of memory -- CAMPE, *s\n");
  82.      printf("Program terminated - 2\n");
  83.      exit(0);
  84.       }
  85.  
  86.    pterm = 0;                                /* no. of product term */
  87.  
  88.    /***
  89.     ***  MINIMIZE ALL MINTERMS WITH ADJACENCY OF 0 & 1 FIRST
  90.     ***/
  91.  
  92.    for (i=0; i<mb; i++)                      /* for adj = 0 or 1 */
  93.       {
  94.      *b = n;                             /* assign back to n */
  95.  
  96.      memcpy(temp, (b+4+nspm*i), nspm);   /* pick a minterm from b-array */
  97.      c = adjacent(temp, a, 1);           /* obtain the adjacent terms */
  98.      adj = *(c+1);                       /* adjacency of minterm */
  99.  
  100.      if (adj <= 1)                       /* adjacency 0 or 1 */
  101.         {
  102.            d = cpt(c);                   /* generate CPT */
  103.  
  104.            s = (unsigned char *) realloc(s,4+n*(pterm+1));   /* more space */
  105.            if (s == 0)
  106.           {
  107.              printf("Out of memory -- CAMPE, *s\n");
  108.              printf("Program terminated - 3\n");
  109.              exit(0);
  110.           }
  111.            memcpy ((s+4+n*pterm),d,n);   /* add CPT to soln array */
  112.            pterm++;                      /* product term count */
  113.  
  114.            b = reduce(c,b,i);            /* remove minterms covered from b-array */
  115.            i = (char) *b;                /* index pointer of b-array */
  116.            mb = *(b+2)<<8 | *(b+1);      /* value of mb altered in b-array */
  117.  
  118.            free(d);                    /* free pointer */
  119.         }
  120.      free(c);                            /* free pointer */
  121.       }
  122.  
  123.     /***
  124.      ***  MINIMIZE MINTERMS WITH ADJACENCY GREATER THAN 1
  125.      ***/
  126.  
  127.     while (mb > 0)
  128.        {
  129.       adj0 = 255;                     /* max for 8-bit */
  130.  
  131.       *b = n;                         /* assign back to n */
  132.  
  133.       /***
  134.        ***  OBTAIN AN ARRAY, PM OF POSITION OF ADJACENT TERMS WITH THE LOWEST
  135.        ***  ADJACENCY WRT B-ARRAY
  136.        ***/
  137.  
  138.       for (i=0; i<mb; i++)
  139.          {
  140.         memcpy(temp, (b+4+nspm*i), nspm);    /* pick a minterm */
  141.         adji = adjacency(temp, b);           /* adj wrt b-array */
  142.  
  143.         if (adji<adj0)                       /* look for lowest adj */
  144.            {
  145.               if (adj0!=255)                 /* not the 1st term */
  146.              free(pm);
  147.  
  148.               adj0 = adji;                   /* update lowest adj */
  149.               count = 1;                        /* set count to 1 */
  150.               pm = (unsigned short *) malloc(2);   /* space for pos */
  151.               if (pm == 0)
  152.              {
  153.                 printf("Out of memory -- CAMPE, *pm\n");
  154.                 printf("Program terminated - 4\n");
  155.                 exit(0);
  156.              }
  157.               *pm = i;                       /* 1st pos of minterm */
  158.            }
  159.  
  160.         else if (adji==adj0)                 /* adjacency as low */
  161.            {
  162.               count++;                       /* no. of elements in pm-array */
  163.               pm = (unsigned short*) realloc(pm, 2*count);    /* more space */
  164.               if (pm == 0)
  165.              {
  166.                 printf("Out of memory -- CAMPE, *pm\n");
  167.                 printf("Program terminated - 5\n");
  168.                 exit(0);
  169.              }
  170.               *(pm+count-1) = i;             /* elements in pm-array */
  171.            }
  172.          }
  173.  
  174.       if (count==1)                  /* if only 1 element in pm-array */
  175.          pos = *pm;
  176.       else
  177.          {
  178.         /***
  179.          ***  FORM PM-ARRAY, SELECT THE 1ST ADJACENT TERM THAT HAS THE
  180.          ***  LOWEST ADJACENCY WRT A-ARRAY
  181.          ***/
  182.  
  183.         adj0 = 255;                      /* max for 8-bit */
  184.         for (i=0; i<count; i++)          /* do for all in pm-array */
  185.            {
  186.               pos1 = *(pm+i);                     /* pos of minterm */
  187.               memcpy(temp, (b+4+nspm*pos1), nspm);
  188.               adji = adjacency(temp, a);          /* adj wrt a-array */
  189.  
  190.               if (adji<adj0)                      /* new lowest found */
  191.              {
  192.                 adj0 = adji;                  /* update lowest */
  193.                 pos = *(pm+i);                /* position of required minterm */
  194.              }
  195.            }
  196.          }
  197.        free(pm);
  198.  
  199.        memcpy(temp, (b+4+nspm*pos), nspm);       /* pick the required minterm */
  200.  
  201.        /***
  202.         ***  GENERATE SSM AND TEST FOR COVERAGE BY FUNCTION
  203.         ***/
  204.  
  205.        c = adjacent(temp, a, 255);          /* obtain adjacent terms */
  206.        adj = *(c+1);                        /* adjacency of minterms */
  207.        d = cpt(c);                          /* generate CPT */
  208.        e = ssm(d);                          /* generate SSM */
  209.        m = topow(*(e+1));                   /* no. of SSM terms */
  210.  
  211.        for (j=0; j<m; j++)                  /* check for SSM coverage */
  212.           {
  213.          memcpy (temp, (e+4+nspm*j), nspm);
  214.          test = exist (temp, a, ma);
  215.          if (test == -1)                /* minterm not in a-array */
  216.              break;
  217.           }
  218.  
  219.        /***
  220.         ***  ALL SSM COVERED BY THE FUNCTION, SELECT CPT AS PRODUCT TERM
  221.         ***/
  222.  
  223.        if (test == 0)                       /* all SSM's covered by fn */
  224.           {
  225.          if (m > 65536)                 /* too many SSM terms */
  226.             {
  227.                printf("Product Term Too Huge \n");
  228.                printf("Program terminated \n");
  229.                exit(0);
  230.             }
  231.          *(e+1) = (m-1) & mask8;        /* no. of SSM terms minus 1 */
  232.          *(e+2) = (m-1)>>8 & mask8;
  233.          b = reduce(e,b,i);             /* remove minterms covered from b-array */
  234.          mb = *(b+2)<<8 | *(b+1);       /* no. of minterms in b-array */
  235.  
  236.          s = (unsigned char *) realloc(s, 4+n*(pterm+1)); /* more space */
  237.          if (s == 0)
  238.             {
  239.                printf("Out of memory -- CAMPE, *s\n");
  240.                printf("Program terminated - 6\n");
  241.                exit(0);
  242.             }
  243.          memcpy((s+4+n*pterm),d,n);     /* add CPT to soln array */
  244.          pterm++;                       /* no. of product terms */
  245.  
  246.          free(d);                       /* free pointers */
  247.          free(e);
  248.           }
  249.        else
  250.           {
  251.          /***
  252.           ***  SSM NOT COVERED BY THE FUNCTION
  253.           ***/
  254.  
  255.          free(d);                       /* free pointer */
  256.          cover =0;                      /* coverage status */
  257.  
  258.          while (!cover)        /* do until shrinked SSM is covered */
  259.             {
  260.                /***
  261.             ***  OBTAIN AN ARRAY, PM OF POSITIONS OF ADJACENT TERMS, MI WITH THE LARGEST WI
  262.             ***/
  263.  
  264.                wo = -1;                        /* initial value */
  265.                for (j=0; j<adj; j++)           /* do for all adjacent terms */
  266.               {
  267.                  memcpy(temp,(c+4+nspm*(j+1)),nspm); /* pick adjacent term */
  268.  
  269.                  wi = adj_of_mi(temp,e,a);           /* obtain wi */
  270.                  if (wi>wo)
  271.                 {
  272.                    if (wo != -1)                 /* not the first */
  273.                       free(pm);
  274.                    wo = wi;                      /* new lowest value */
  275.                    count = 1;                    /* reset count to 1 */
  276.  
  277.                    pm = (unsigned short *) malloc(2);  /* space for pm */
  278.                    if (pm==0)
  279.                       {
  280.                      printf("Out of memory -- CAMPE, *pm\n");
  281.                      printf("Program terminated - 7\n");
  282.                      exit(0);
  283.                       }
  284.                    *pm = j;                      /* first element */
  285.                 }
  286.                  else if (wi==wo)                    /* wi as large */
  287.                 {
  288.                    count++;                      /* no. of element in pm-array */
  289.  
  290.                    pm = (unsigned short *) realloc (pm, 2*count);  /* more space */
  291.                    if (pm==0)
  292.                       {
  293.                      printf("Out of memory -- CAMPE, *pm\n");
  294.                      printf("Program terminated - 8\n");
  295.                      exit(0);
  296.                       }
  297.                    *(pm+count-1) = j;            /* elements of pm-array */
  298.                 }
  299.               }
  300.                free(e);                                  /* free pointer */
  301.  
  302.                /***
  303.             ***  DETERMINE FROM PM-ARRAY, AN ARRAY, PM1 OF POSITION OF ADJACENT TERMS
  304.             ***  THAT HAVE ALREADY BEEN COVERED
  305.             ***/
  306.  
  307.                count1 = count;                           /* no. of elements in pm & pm1 */
  308.  
  309.                pm1 = (unsigned short *) malloc(2);       /* space for pm1 */
  310.                if (pm1==0)
  311.               {
  312.                  printf("Out of memory -- CAMPE, *pm1\n");
  313.                  printf("Program terminated - 9\n");
  314.                  exit(0);
  315.               }
  316.  
  317.                k = 0;
  318.                for (j=0; j<count; j++)              /* do for all element in pm-array */
  319.               {
  320.                  memcpy(temp, (c+4+nspm*(1+ *(pm+j))), nspm);    /* pick adj term */
  321.                  test = exist(temp,b,mb);
  322.                  if (test == -1)                               /* already covered */
  323.                 {
  324.                    memcpy((pm1+k++), (pm+j), 2);       /* transfer pos to pm1 */
  325.                    count1--;                                       /* reduced */
  326.  
  327.                    pm1 = (unsigned short *) realloc(pm1, 2*(k+1)); /*  more space */
  328.                    if (pm1==0)
  329.                       {
  330.                      printf("Out of memory -- CAMPE, *pm1\n");
  331.                      printf("Program terminated - 10\n");
  332.                      exit(0);
  333.                       }
  334.                 }
  335.               }
  336.  
  337.                /***
  338.             ***  SELECT FROM PM1-ARRAY, THE 1ST ADJACENT TERM, MI WITH THE LOWEST
  339.             ***  ADJACENCY WRT B-ARRAY
  340.             ***/
  341.  
  342.                adj0 = 255;                     /* max of 8-bit */
  343.                for (j=0; j<k; j++)             /* do for all in pm1-array */
  344.               {
  345.                  memcpy(temp,(c+4+nspm*(1+*(pm1+j))),nspm);  /* pick an adj term */
  346.                  adji = adjacency(temp,b);                   /* obtain adjacency */
  347.  
  348.                  if (adji<adj0)                        /* new lowest found */
  349.                 {
  350.                    adj0 = adji;                    /* update lowest */
  351.                    pos = *(pm1+j);                 /* position of lowest adj term */
  352.                 }
  353.               }
  354.  
  355.                if (count1==count)                /* select the 1st if all not covered */
  356.               pos = *pm;
  357.  
  358.                adj--;                            /* no. of adj terms in c-array */
  359.                *(c+1) = adj;                     /* adjacency after shrinking CPT */
  360.  
  361.                free(pm);                         /* free pointer */
  362.                free(pm1);
  363.  
  364.                /***
  365.             ***  SHRINK CPT (REMOVE 1 ADJACENT TERM FROM C-ARRAY), GENERATE NEW CPT, SSM
  366.             ***  AND TEST FOR COVERAGE BY FUNCTION
  367.             ***/
  368.  
  369.                memcpy((c+4+nspm*(1+pos)), (c+4+nspm*(2+pos)), nspm*(adj-pos));  /* shrink CPT */
  370.  
  371.                c = (unsigned char*) realloc(c, 4+nspm*(1+adj));   /* reduce space */
  372.                if (c==0)
  373.               {
  374.                  printf("Out of memory -- CAMPE, *c\n");
  375.                  printf("Program terminated - 11\n");
  376.                  exit(0);
  377.               }
  378.  
  379.                d = cpt(c);                  /* generate CPT */
  380.                e = ssm(d);                  /* generate SSM */
  381.                m = topow(*(e+1));           /* no. of SSM terms */
  382.  
  383.                for (k=0; k<m; k++)          /* check SSM coverage by function */
  384.               {
  385.                  memcpy(temp, (e+4+nspm*k), nspm);  /* pick SSM term */
  386.                  test = exist(temp,a,ma);
  387.                  if (test == -1)                    /* SSM term not covered */
  388.                 break;
  389.               }
  390.  
  391.                /***
  392.             ***  SHRINKED SSM COVERED BY FUNCTION, REDUCE B-ARRAY
  393.             ***  AND SELECT SHRINKED CPT AS PRODUCT TERM
  394.             ***/
  395.  
  396.                if (test==0)                   /* SSM covered */
  397.               {
  398.                  if (m > 65536)           /* too many SSM terms */
  399.                 {
  400.                    printf("Product Term Too Huge \n");
  401.                    printf("Program terminated \n");
  402.                    exit(0);
  403.                 }
  404.                  *(e+1) = (m-1) & mask8;  /* no. of SSM terms minus 1 */
  405.                  *(e+2) = (m-1)>>8 & mask8;
  406.                  b = reduce(e, b, i);     /* remove minterms covered from b-array */
  407.                  mb = *(b+2)<<8 | *(b+1); /* no. of minterms in b-array */
  408.  
  409.                  s = (unsigned char *) realloc(s, 4+n*(pterm+1));  /* more space */
  410.                  if (s==0)
  411.                 {
  412.                    printf("Out of memory -- CAMPE, *s\n");
  413.                    printf("Program terminated - 12\n");
  414.                    exit(0);
  415.                 }
  416.                  memcpy ((s+4+n*pterm), d, n);      /* add shrinked CPT to soln array */
  417.                  pterm++;                           /* no. of product terms */
  418.  
  419.                  cover = 1;                         /* coverage status */
  420.                  free(e);                           /* free pointer */
  421.               }
  422.                free (d);                      /* free pointer */
  423.             }
  424.           }
  425.       free(c);                   /* free pointer */
  426.       }
  427.    *s = n;                           /* no. of variables */
  428.    *(s+1) = pterm & mask8;           /* no. of product terms in 2 bytes */
  429.    *(s+2) = pterm>>8 & mask8;
  430.  
  431.    free(temp);                       /* free pointer */
  432.    free(a);
  433.    free(b);
  434.  
  435.    return(s);                        /* return solution array */
  436. }
  437.  
  438.